package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	//"strings"
	//"io/ioutil"
	"log"
	"sync"
	//"net/http"
)

func main() {
	var urls = make([]string, 23429)
	var workers int = 1000
	ch := make(chan string)
	var w sync.WaitGroup
	w.Add(workers)
	for i := 0; i < workers; i++ {
		go readUrl(ch, &w)
	}
	log.Printf("Start scanning urls \n")
	for i := range urls {
		urls[i] = fmt.Sprintf("http://www.zanmeishi.com/tab/%d.html", i)
		ch <- urls[i]
	}
	log.Printf("urls is %v", urls)
}

func readUrl(ch chan string, w *sync.WaitGroup) {
	for url := range ch {
		if len(url) == 0 {
			w.Done()
			break
		}
		doc, err := goquery.NewDocument(url)
		if err != nil {
			log.Fatal(err)
		}
		if resourceUrl, err := doc.Find(".notice a").Next().Attr("href"); !err {
			log.Printf("resource is %s \n", resourceUrl)
		} else {

		}
	}
}
